package com.example.idachuappone.person.member;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.person.entity.MemberYuE;
import com.example.idachuappone.person.entity.MemberYuEDetail;
import com.example.idachuappone.person.method.UserInfoMethod;
import com.example.idachuappone.person.method.UserInfoMethod.CallBackString;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;

public class MemberYuEDetailActivity extends BaseActivity implements OnClickListener {
	private Button btn_back;
	private Dialog dialog;
	private ListView lv_yue_item;
	private MyMemberAdapter memberAdapter;
	private MemberYuE memberYuE;

	private class MyMemberAdapter extends BaseAdapter {
		private Context context;
		private List<MemberYuEDetail> list;

		public MyMemberAdapter(Context context) {
			this.context = context;
		}

		public void setList(List<MemberYuEDetail> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return this.list == null ? null : (MemberYuEDetail) this.list.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = LinearLayout.inflate(this.context, R.layout.simple_member_yue_detail_item, null);
				viewHolder = new ViewHolder();
				viewHolder.tv_money_number = (TextView) convertView.findViewById(R.id.tv_money_number);
				viewHolder.tv_yue_type = (TextView) convertView.findViewById(R.id.tv_yue_type);
				viewHolder.tv_yue_date = (TextView) convertView.findViewById(R.id.tv_yue_date);
				viewHolder.img_jia_jian = (ImageView) convertView.findViewById(R.id.img_jia_jian);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			MemberYuEDetail memberYuEDetail = this.list.get(position);
			viewHolder.tv_money_number.setTextColor(Color.parseColor("#009944"));
			if (memberYuEDetail.getOperation().equals(Constant.MEMBER_EXPEND)) {
				viewHolder.tv_yue_type.setText("\u6d88\u8d39");
				viewHolder.tv_money_number.setText(ComUtil.FormatDistance(memberYuEDetail.getTotal_fee().replace("-",
						"")));
				viewHolder.img_jia_jian.setImageResource(R.drawable.img_member_yue_detail_jian);
				viewHolder.tv_money_number.setTextColor(MemberYuEDetailActivity.this.getResources().getColor(
						R.color.txt_default_color));
			} else if (memberYuEDetail.getOperation().equals(Constant.MEMBER_PRESENT)) {
				viewHolder.tv_yue_type.setText("\u8d60\u9001");
				viewHolder.tv_money_number.setText(ComUtil.FormatDistance(memberYuEDetail.getTotal_fee().replace("-",
						"")));
				viewHolder.img_jia_jian.setImageResource(R.drawable.img_member_yue_detail_jia);
			} else if (memberYuEDetail.getOperation().equals(Constant.MEMBER_RECHARGE)) {
				viewHolder.tv_yue_type.setText("\u5145\u503c");
				viewHolder.tv_money_number.setText(ComUtil.FormatDistance(memberYuEDetail.getTotal_fee().replace("-",
						"")));
				viewHolder.img_jia_jian.setImageResource(R.drawable.img_member_yue_detail_jia);
			} else if (memberYuEDetail.getOperation().equals(Constant.MEMBER_REFUND)) {
				viewHolder.tv_yue_type.setText("\u9000\u6b3e");
				viewHolder.tv_money_number.setText(ComUtil.FormatDistance(memberYuEDetail.getTotal_fee().replace("-",
						"")));
				viewHolder.img_jia_jian.setImageResource(R.drawable.img_member_yue_detail_jia);
			}
			viewHolder.tv_yue_date.setText(memberYuEDetail.getDateline().substring(0,
					memberYuEDetail.getDateline().indexOf(" ")));
			return convertView;
		}
	}

	private class ViewHolder {
		private ImageView img_jia_jian;
		private TextView tv_money_number;
		private TextView tv_yue_date;
		private TextView tv_yue_type;

		private ViewHolder() {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_yu_edetail);
		this.dialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
		initData();
	}

	private void initData() {
		this.dialog.show();
		UserInfoMethod.getUserYuEDetail(this, new CallBackString() {
			@Override
			public void onSuccess(String result) {
				MemberYuEDetailActivity.this.dialog.dismiss();
				try {
					JSONObject jsonObject = new JSONObject(result);
					if (jsonObject.getInt("code") == 10000) {
						MemberYuEDetailActivity.this.memberYuE = new MemberYuE();
						MemberYuEDetailActivity.this.memberYuE.parseJson(jsonObject.getJSONObject("data"));
						MemberYuEDetailActivity.this.initAdapter();
						return;
					}
					MainToast.show(MemberYuEDetailActivity.this, jsonObject.getString("error"), 0);
				} catch (JSONException e) {
					e.printStackTrace();
					MainToast.show(MemberYuEDetailActivity.this,
							MemberYuEDetailActivity.this.getResources().getString(R.string.app_error), 0);
				}
			}

			@Override
			public void onFail() {
				MemberYuEDetailActivity.this.dialog.dismiss();
				MainToast.show(MemberYuEDetailActivity.this,
						MemberYuEDetailActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}

	private void initAdapter() {
		this.memberAdapter.setList(this.memberYuE.getResult());
		this.memberAdapter.notifyDataSetChanged();
	}

	private void initView() {
		this.lv_yue_item = (ListView) findViewById(R.id.lv_yue_item);
		this.memberAdapter = new MyMemberAdapter(this);
		this.lv_yue_item.setAdapter(this.memberAdapter);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		default:
		}
	}
}
